/**
 * Created by kevin on 2017/6/13.
 */

var request = require('request');

var dingding_robot_url = 'https://oapi.dingtalk.com/robot/send?access_token=';

var website = require('../utils/website');

function dingding() {

}

dingding.sendTextMessage = function (token,message) {
    if (!token) return;
    request({
        url: dingding_robot_url + token,
        method: 'POST',
        json: true,
        headers: { 'content-type': 'application/json'},
        body: {
            msgtype:'text',
            text: {
                content: message
            },
            at: {
                isAtAll:true
            }
        }
    },function (err,response) {
        if (err) {
            console.log('err = ' + err);
        } else  {
            console.log('response = ' + response);
        }
    });
};

dingding.sendMarkdownMessage = function (token,title,message) {
    console.log('开始发送钉钉消息');
    if (!token) return;
    request({
        url: dingding_robot_url + token,
        method: 'POST',
        json: true,
        headers: { 'content-type': 'application/json'},
        body: {
            msgtype:'markdown',
            markdown: {
                title: title,
                text: message
            },
            at: {
                isAtAll:true
            }
        }
    },function (err,response) {
        if (err) {
            console.log('err = ' + err);
        } else  {
            console.log('response = ' + JSON.stringify(response));
        }
    });
};

dingding.sendPackageStart = function (req,token,app) {
    var platformValue = (app.type === 1 ? 'iOS' : (app.type === 2 ? 'Android' : 'unknown'));
    var title = `#### ${app.name} 开始打包啦\n`;
    var version = `###### 版本：${app.version}(${app.build})\n`;
    var platform = `###### 平台：${platformValue}\n`;
    let iconUrl = website.url(req) + app.iconUrl;
    var img = `###### ![screenshot](${iconUrl})\n`;
    var other = `###### [${website.webname()}](${website.url()}) \n`;
    var text = title + img + version + platform + other;
    this.sendMarkdownMessage(token,app.name+'开始打包啦',text);
};

dingding.sendPackageSuccess = function (req,token,package) {
    var platformValue = (package.type === 1 ? 'iOS' : (package.type === 2 ? 'Android' : 'unknown'));
    var title = `#### ${package.app.name} 有新包发布啦\n`;
    var version = `###### 版本：${package.version}(${package.build})\n`;
    var platform = `###### 平台：${platformValue}\n`;
    var user = `###### 操作：${package.user.nickname}\n\n`;
    var downloadUrl = website.url(req) + '/package/download/'+package._id;
    var qrcodeImage = website.url(req) + '/qrcode?url=' + downloadUrl;
    var img = `###### ![screenshot](${qrcodeImage})\n`;
    var other = `###### 手机扫描上方二维码安装,\n ###### 或者[点击此处](${downloadUrl})查看详情，\n ###### 如发现问题请联系开发人员。\n　\n###### [${website.webname()}](${website.url()}) \n`;
    var text = title + version + platform + user + img + other;
    this.sendMarkdownMessage(token,package.app.name+'有新包啦',text);
};

module.exports = dingding;
